www.gusucode.com > Phased Array System Toolbox Add-On for Demorad 工具箱matlab源码程序 > Phased Array System Toolbox Add-On for Demorad/demorad/demoradexamples/SaveRadarIQDataToFileAcquiredViaDemoradExample.m

    %% Save Radar I/Q Data Acquired via Demorad Radar Sensor Platform to a File

% Copyright 2019 The MathWorks, Inc
%% Introduction
% In this example, we use the Demorad Radar Sensor Platform to send and receive
% FMCW chirps, and perform range-Doppler processing on the returns. This is made
% possible by using the Phased Array System Toolbox(TM) in conjunction with the
% Phased Array System Toolbox Add-On for Demorad.
%
% The samples are processed using the visualized in a range-Doppler scope, a component
% of the Phased Array System Toolbox(TM). Before range-Doppler processing, MTI
% processing is used to remove clutter, followed by beamforming of the result to
% improve the SNR.
%
% The user has the option to write the raw dechirped samples to a binary file
% for off-line processing, instructions for which can be found
% later in the example.

%% Radar System Parameters
% In this section, radar settings are defined following the constraints of the
% hardware vendor. These parameters are set directly from MATLAB, and will be
% sent to the Demorad platform.

demorad = DemoradBoard;
demorad.AcquisitionTime = 20; % Acquire samples for 20 seconds
demorad.NumChirps = 32;
demorad.WindowMethod = 'Rectangular';

setup(demorad); % send parameters to the Demorad

%% Initialize the Signal Processing Components
% I/Q samples are returned as a radar data cube. We utilize a 3-pulse canceller to
% to perform moving target indication (MTI), and a Phase Shift Beamformer to 
% beamform the result. Range-Doppler processing is performed on the beamformed
% data, and visualized in a range-Doppler scope. The scope shows a slow-moving
% target moving back and forth in front of the radar between 0 and 5 meters.

numChirps = demorad.NumChirps - 2; % Drop first two pulses
receiveElementSpacing = 0.0062; %demorad.ReceiveElementSpacing;
rangeNFFT = 256;
dopplerNFFT = 256;

% Use a Taylor window for spatial tapering. This is the conventional window for
% radar applications of this type
spatialTaper = taylorwin(4,4,-40); 
spatialTaper = spatialTaper./norm(spatialTaper); % Normalize taper

array = phased.ULA('NumElements',demorad.NumChannels, ...
  'ElementSpacing',receiveElementSpacing);

beamFormer = phased.PhaseShiftBeamformer('SensorArray',array, ...
  'OperatingFrequency',demorad.CenterFrequency,'Direction',[0;0]);

wfMetadata = demorad.Metadata;
rdResponse = phased.RangeDopplerResponse(...
  'RangeMethod','FFT',...
  'SampleRate',demorad.SampleRate, ...
  'RangeFFTLengthSource','Property',...
  'RangeFFTLength',rangeNFFT,...
  'ReferenceRangeCentered',false, ...
  'DopplerFFTLengthSource','Property',...
  'DopplerFFTLength', dopplerNFFT, ...
  'DopplerWindow','Taylor',...
  'DopplerSidelobeAttenuation',40,...
  'DopplerOutput','Speed','OperatingFrequency',demorad.CenterFrequency, ...
  'SweepSlope',wfMetadata.SweepSlope);

% Set colorbar limits from -20 to 0 dB. This range was chosen based on the
% return from the testing environment with no target present, and may need to be
% changed depending on the environment.
dataLimits = 10.^([0 -10]./20);
scope = phased.RangeDopplerScope(...
  'IQDataInput',false);

%%
% *Initialize Binary File Writer*
%
% This section initializes the binary file writer if we choose to log the
% radar echoes. To enable the optional logging, change the below 'loggingEchoes'
% flag to 'true'.

loggingEchoes = false;
if loggingEchoes
  filename = 'DemoradRawIQSamples.bb'; %#ok<UNRCH>
  writedata = RadarBasebandFileWriter(filename,demorad.SampleRate, ...
    demorad.CenterFrequency);
  writedata.Metadata = wfMetadata;
end

%% Data Acquisition and Visualization Loop
% This performs the entire process of reading data from the Demorad Platform,
% writing it to a file, and visualizing the range-Doppler response.
while ~isDone(demorad)
  
  % Receive sampled radar echoes from the Demorad as a 3-D data cube
  iq = demorad();
  
  % Perform MTI processing and drop first two pulses due to filter effects
  declutterIQ = filter([1 -2 1],1,iq,[],3);
  declutterIQ = declutterIQ(:,:,3:end); % fast-time x spatial x slow-time
  
  % Convert to a 2-D matrix and beamform along the 2nd (spatial) dimension
  declutterIQ = radarcube2mat(declutterIQ,demorad.NumChannels); % fast-time*slow-time x spatial
  declutterIQ = declutterIQ.*spatialTaper.'; % Spatial taper
  bfIQ = beamFormer(declutterIQ);
  
  % Reshape into a 2-D matrix and perform range-Doppler processing
  bfIQ = reshape(bfIQ,rangeNFFT,numChirps); % fast-time x slow-time
  
  % Range-Doppler Process
  [pcDpBfIQ,rngVec,dopVec]  = rdResponse(bfIQ);
  
  % Normalize to max
  pcDpBfIQ = pcDpBfIQ./max(abs(pcDpBfIQ(:)));
  
  % Limit data to a range (controls colorbar)
  pcDpBfIQ(abs(pcDpBfIQ(:))>dataLimits(1)) = dataLimits(1);
  pcDpBfIQ(abs(pcDpBfIQ(:))<dataLimits(2)) = dataLimits(2);
  
  % Update scope. Only view ranges from 0 to 20 meters
  [~,rangeIdx] = min(abs(rngVec-20));
  scope(pcDpBfIQ(2:rangeIdx,:),rngVec(2:rangeIdx),dopVec);
  pause(0.2);
  
  if loggingEchoes
    % Reshape and write the received I/Q samples to the binary file. We reshape
    % the cube to be the 2-dimensional format accepted by the binary file writer.
    reshapedIQ = radarcube2mat(iq,demorad.NumChannels); %#ok<UNRCH>
    writedata(reshapedIQ);
  end
end

function y = radarcube2mat(x,ncols)
% Reshape 3-D radar data cube to 2-D matrix where each column is an element.
y = reshape(permute(x,[1 3 2]),[],ncols);
end